#-*-coding:utf8-*-

import sys
import json
# sys.path.append("./pyrcd/personal_recommendation")
sys.path.append('/home/lili/project/recommend/pyrcd/personal_recommendation')
import CF.production.itemcf as itemcf
import LFM.production.lfm as lfmt
import PR.production.personal_rank as pr
import Item2Vec.production.produce_item_sim as i2v
from cgi import parse_qs, escape

import LR.util.test_cvs as tc
import LR.util.lrsort as lrs
import LR.production.check as ck
import random


#itemcf
def CF(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])

    # Returns a dictionary containing lists as values.
    d = parse_qs(environ['QUERY_STRING'])

    # In this idiom you must issue a list containing a default value.
    uid = d.get('uid', [''])[0]  # Returns the first age value.
    print uid

    # print main_flow()
    # return main_flow()
    # item_info = get_item_info("../data/movies.txt")
    # print item_info
    res=itemcf.web_return(str(uid))

    # lr排序
    # res = lrs.lrsort(res, uid)
    # res[str(uid)] = random.shuffle(res[str(uid)])
    # print res[str(uid)]
    res[str(uid)] = sorted(res[str(uid)].items(), key=lambda item: item[1])
    # print res
    random.shuffle(res[str(uid)])
    # print res
    str_json = json.dumps(res)
    # print str_json
    return str_json


#lfm
def lfm(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])

    # Returns a dictionary containing lists as values.
    d = parse_qs(environ['QUERY_STRING'])

    # In this idiom you must issue a list containing a default value.
    uid = d.get('uid', [''])[0]  # Returns the first age value.
    print uid

    # print main_flow()
    # return main_flow()
    # item_info = get_item_info("../data/movies.txt")
    # print item_info
    # print lfmt
    res=lfmt.web_return(str(uid))
    # lr排序
    res = lrs.lrsort(res, uid)
    # print res


    str_json = json.dumps(res)
    return str_json

#personal_rank
def personal_rank(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])

    # Returns a dictionary containing lists as values.
    d = parse_qs(environ['QUERY_STRING'])

    # In this idiom you must issue a list containing a default value.
    uid = d.get('uid', [''])[0]  # Returns the first age value.
    print uid


    res=pr.web_return(str(uid))
    # lr排序
    res = lrs.lrsort(res, uid)


    str_json = json.dumps(res)
    return str_json

#item2Vec
def item2Vec(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])

    # Returns a dictionary containing lists as values.
    d = parse_qs(environ['QUERY_STRING'])

    # In this idiom you must issue a list containing a default value.
    uid = d.get('uid', [''])[0]  # Returns the first age value.
    print uid


    res=i2v.web_return(str(uid))
    # lr排序
    res = lrs.lrsort(res, uid)


    str_json = json.dumps(res)
    return str_json